/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

const tools = require("../../js/tools");

window.onload = function () {
    tools.LoadToken();

    if (document.getElementById("student-index-page") !== null) {
        selectStudentMenuNavBar();
        listenLogoutClick();
        tools.CheckTokenExist();
        tools.ConfirmLeavePage();
    }
    if (document.getElementById("student-course-page") !== null) {
        selectCourseNavBar();
    }
    if (document.getElementById("student-course-chapters-page") !== null) {
        setChapterPageTitle();
    }
    if (document.getElementById("student-course-content-page") !== null) {
        setContentPageTitle();
        displayMarkdown();
    }
};

function selectStudentMenuNavBar() {
    let navURL = {
        "navCourse": "student-course.html",
        "navPractice": "student-practice.html",
        "navHomework": "student-homework.html",
        "navExam": "student-exam.html",
        "navData": "student-data.html",
        "navSetting": "setting.html"
    };

    let navLinkList = [
        "navCourse",
        "navPractice",
        "navHomework",
        "navExam",
        "navData",
        "navSetting"
    ];

    let iframe = document.getElementById("iframeContainer");
    if (iframe === null) {
        return;
    }
    if (iframe.src !== "student-course.html") {
        iframe.src = "student-course.html";
    }

    function doListenClick(id) {
        for (let i = 0; i < navLinkList.length; i++) {
            let e = document.getElementById(navLinkList[i]);
            if (e === null) {
                return
            }
            e.classList.remove("bg-info");
            if (navLinkList[i] !== "navSetting") {
                e.classList.add("bg-primary");
            } else {
                e.classList.add("bg-secondary");
            }
        }

        let e = document.getElementById(id);
        if (e === null) {
            return
        }
        if (id !== "navSetting") {
            e.classList.remove("bg-primary");
        } else {
            e.classList.remove("bg-secondary");
        }
        e.classList.add("bg-info");
        iframe.src = navURL[id];
    }

    for (let i = 0; i < navLinkList.length; i++) {
        let id = navLinkList[i];
        let e = document.getElementById(id);
        if (e === null) {
            return
        }
        e.onclick = function () {
            doListenClick(id)
        };
    }
}

function selectCourseNavBar() {
    let elIDs = [
        "courseCollege",
        "courseMajor",
        "courseClass"
    ];

    let doListen = function (id) {
        for (let i = 0; i < elIDs.length; i++) {
            let e = document.getElementById(elIDs[i]);
            if (e === null) {
                return
            }
            e.className = "text-secondary";
        }
        let l = document.getElementById(id);
        if (l === null) {
            return;
        }
        l.className = "text-primary"
    };

    for (let i = 0; i < elIDs.length; i++) {
        let e = document.getElementById(elIDs[i]);
        if (e === null) {
            return
        }
        e.onclick = function () {
            let id = elIDs[i];
            doListen(id)
        }
    }
}

function listenLogoutClick() {
    let e = document.getElementById("navLogout");
    if (e === null) {
        return
    }
    e.onclick = function () {
        tools.RemoveCookie("AuthToken");
        window.location.href = "/index.html";
    }
}

function displayMarkdown() {
    let elMarkdown = document.getElementById("markdown");
    let elMain = document.getElementById("main");
    if (elMarkdown === null || elMain === null) {
        return
    }

    let markdown = elMarkdown.innerText;
    elMain.innerHTML = tools.CompileMarkdown(markdown);
}

function setChapterPageTitle() {
    let title = tools.GetQueryString("courseTitle");
    let e = document.getElementById("chapterTitle");
    if (e === null) {
        return
    }
    e.innerHTML = title;
}

function setContentPageTitle() {
    let courseTitle = tools.GetQueryString("courseTitle");
    let chapterTitle = tools.GetQueryString("chapterTitle");
    let e = document.getElementById("contentTitle");
    if (e === null) {
        return
    }
    e.innerHTML = courseTitle + "&nbsp;&nbsp;-&nbsp;&nbsp;" + chapterTitle;
}
